package com.tarena.demo.luban.all.main.controller;


import com.tarena.demo.luban.all.main.service.OrderService;
import com.tarena.demo.luban.commons.restful.JsonResult;
import com.tarena.demo.luban.protocol.order.param.OrderAddParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/base/order")
@Api(tags = "订单")
public class OrderController {
//    @Autowired
//    private OrderService orderService;

    @Autowired
    RocketMQTemplate rocketMQTemplate;

    @PostMapping("/add")
    @ApiOperation("新增订单的功能")
    public JsonResult addOrder(OrderAddParam orderAddParam){
//        orderService.addOrder(orderAddParam);
        //1.组织消息对象
        Message<OrderAddParam> message =
                MessageBuilder.withPayload(orderAddParam).build();
        //2.使用异步发送消息
        rocketMQTemplate.asyncSend("order-test-topic", message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("发送成功...");
            }
            @Override
            public void onException(Throwable throwable) {
                System.out.println("发送失败!!!");
            }
        });

        return JsonResult.ok("新增订单完成!");
    }

}


